package mspbots.data.tl.prod.quickbooks.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.quickbooks.mapper.OnlineInvoiceMapper;
import mspbots.data.tl.prod.quickbooks.model.OnlineInvoice;
import mspbots.data.tl.prod.quickbooks.service.OnlineInvoiceService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Jay.Yang
 */

@Service
@Log4j2
@DS("master")
public class OnlineInvoiceServiceImpl extends AbstractBaseServiceImpl<OnlineInvoiceMapper, OnlineInvoice>
        implements OnlineInvoiceService {

    @Override
    public void sync(ResultEntity resultEntity) {
        JsonNode entity = resultEntity.getEntity();
        OnlineInvoice data = OnlineInvoice.builder()
                .invoiceId(ObjectUtils.isEmpty(entity.get("Id")) ? null : entity.get("Id").asInt())
                .balance(ObjectUtils.isEmpty(entity.get("Balance")) ? null : entity.get("Balance").asDouble())
                .customerRefName(ObjectUtils.isEmpty(entity.get("CustomerRef")) ? null :
                        ObjectUtils.isEmpty(entity.get("CustomerRef").get("name")) ? null : entity.get("CustomerRef").get("name").asText())
                .docNumber(ObjectUtils.isEmpty(entity.get("DocNumber")) ? null : entity.get("DocNumber").asInt())
                .dueDate(ObjectUtils.isEmpty(entity.get("DueDate")) ? null : entity.get("DueDate").asText())
                .emailStatus(ObjectUtils.isEmpty(entity.get("EmailStatus")) ? null : entity.get("EmailStatus").asText())
                .printStatus(ObjectUtils.isEmpty(entity.get("PrintStatus")) ? null : entity.get("PrintStatus").asText())
                .totalAmt(ObjectUtils.isEmpty(entity.get("TotalAmt")) ? null : entity.get("TotalAmt").asDouble())
                .totalTax(ObjectUtils.isEmpty(entity.get("TxnTaxDetail")) ? null :
                        ObjectUtils.isEmpty(entity.get("TxnTaxDetail").get("TotalTax")) ? null : entity.get("TxnTaxDetail").get("TotalTax").asDouble())
                .txnDate(ObjectUtils.isEmpty(entity.get("TxnDate")) ? null : entity.get("TxnDate").asText())
                .syncTime(resultEntity.getSyncDate())
                .build();

        if (!ObjectUtils.isEmpty(entity.get("MetaData"))){
            ZonedDateTime createTime = ZonedDateTime.parse(entity.get("MetaData").get("CreateTime").asText(),
                    DateTimeFormatter.ISO_OFFSET_DATE_TIME).withZoneSameInstant(ZoneOffset.UTC);
            ZonedDateTime LastUpdatedTime = ZonedDateTime.parse(entity.get("MetaData").get("LastUpdatedTime").asText(),
                    DateTimeFormatter.ISO_OFFSET_DATE_TIME).withZoneSameInstant(ZoneOffset.UTC);
            data.setCreateTime(createTime.toLocalDateTime());
            data.setLastUpdatedTime(LastUpdatedTime.toLocalDateTime());
        }
        data.setTenantCode(resultEntity.getTenantId());

        OnlineInvoice dbData = super.baseMapper.selectOne(Wrappers.<OnlineInvoice>lambdaQuery()
                .eq(OnlineInvoice::getTenantCode, data.getTenantCode())
                .eq(OnlineInvoice::getInvoiceId, data.getInvoiceId())
                .last("limit 1"));

        if (ObjectUtils.isEmpty(dbData)){
            super.baseMapper.insert(data);
        }else{
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }

    }
}
